package org.dormi.learn.utils;

import org.dormi.learn.definition.Edge;
import org.dormi.learn.definition.Graph;
import org.dormi.learn.definition.Vertex;

import java.util.ArrayList;
import java.util.List;

/**
 * @author : dormi330
 * @since : 2021/1/27/0027
 */
public class GraphGenerator {

    public static Graph v3_2() {
        Graph g = new Graph();
        g.addEdge(fromString("v0", "v1:1"));
        g.addEdge(fromString("v1", "v2:1, v3:9"));
        g.addEdge(fromString("v3", "v4:1, v0:9"));
        g.addEdge(fromString("v5", "v6:1"));
        g.addEdge(fromString("v6", "v3:1"));

        g.setEntry(new Vertex("v0"));
        return g;
    }

    public static Graph v4_2() {
        Graph g = new Graph();
        g.addEdge(fromString("v0", "v1:1"));
        g.addEdge(fromString("v1", "v2:1, v3:9"));
        g.addEdge(fromString("v3", "v4:1, v0:9"));
        g.addEdge(fromString("v4", "v8:1"));
        g.addEdge(fromString("v5", "v6:1"));
        g.addEdge(fromString("v6", "v3:1,v7:9"));
        g.addEdge(fromString("v7", "v4:1,v8:9"));
        g.addEdge(fromString("v8", "v3:1"));

        g.setEntry(new Vertex("v5"));
        return g;
    }

    /**
     * v1->v2 w=1
     * v1->v3 w=3
     * v1->v4 w=5
     *
     * @param src     v1
     * @param manyDst v2:1, v3:2, v4:5
     */
    private static List<Edge> fromString(String src, String manyDst) {
        String[] allDest = manyDst.split(",");
        List<Edge> edges = new ArrayList<>(allDest.length);
        Vertex srcV = new Vertex(src);
        for (int i = 0; i < allDest.length; i++) {
            String vw = allDest[i];
            Vertex v = new Vertex(vw.split(":")[0].trim());
            int w = Integer.parseInt(vw.split(":")[1].trim());
            edges.add(new Edge(srcV, v, w));
        }
        return edges;
    }

}
